﻿using HandyControl.Controls;
using Serilog;
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;

namespace EditPlugin
{
    /// <summary>
    /// GltfOptimizer.xaml 的交互逻辑
    /// </summary>
    public partial class GltfOptimizer
    {
        public GltfOptimizer()
        {
            InitializeComponent();
            DefButton_Click(null, null);
        }
        private void DefButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            var settings = new GltfOptimizerSettings();
            GltfOptimizerApi.Default(ref settings);

            pos_bits.Value = settings.pos_bits;
            tex_bits.Value = settings.tex_bits;
            col_bits.Value = settings.col_bits;
            trn_bits.Value = settings.trn_bits;
            rot_bits.Value = settings.rot_bits;
            scl_bits.Value = settings.scl_bits;
            anim_freq.Value = settings.anim_freq;
            simplify_threshold.Value = settings.simplify_threshold;
            texture_scale.Value = settings.texture_scale;
            texture_limit.Value = settings.texture_limit;

            pos_normalized.IsChecked = settings.pos_normalized;
            pos_float.IsChecked = settings.pos_float;
            anim_const.IsChecked = settings.anim_const;
            keep_nodes.IsChecked = settings.keep_nodes;
            keep_materials.IsChecked = settings.keep_materials;
            keep_extras.IsChecked = settings.keep_extras;
            mesh_merge.IsChecked = settings.mesh_merge;
            mesh_instancing.IsChecked = settings.mesh_instancing;

            simplify_aggressive.IsChecked = settings.simplify_aggressive;
            simplify_lock_borders.IsChecked = settings.simplify_lock_borders;
            texture_ktx2.IsChecked = settings.texture_ktx2;
            texture_pow2.IsChecked = settings.texture_pow2;
            texture_flipy.IsChecked = settings.texture_flipy;
            quantize.IsChecked = settings.quantize;
            compress.IsChecked = settings.compress;
            compressmore.IsChecked = settings.compressmore;
            fallback.IsChecked = settings.fallback;
        }
        private void OnProcessCompleted(int rc, Exception ex)
        {
            if (Dispatcher.CheckAccess())
            {
                loading.Visibility = System.Windows.Visibility.Collapsed;
                if (ex != null)
                    MessageBox.Error(ex.Message);
                else if (rc != 0)
                {
                    MessageBox.Error($"简化失败: {rc}");
                }
                else
                {
                    MessageBox.Success($"简化成功.");
                }
            }
            else
            {
                Dispatcher.Invoke(() => { OnProcessCompleted(rc, ex); });
            }
        }
        private void OptButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            var srcPath = src.Text;
            var dstPath = dst.Text;

            if (string.IsNullOrEmpty(srcPath))
            {
                MessageBox.Warning("请选择源文件");
                return;
            }
            if (!File.Exists(srcPath))
            {
                MessageBox.Warning("源文件不存在");
                return;
            }
            if (string.IsNullOrEmpty(dstPath))
            {
                MessageBox.Warning("请选择目标文件");
                return;
            }

            var settings = new GltfOptimizerSettings();
            GltfOptimizerApi.Default(ref settings);

            settings.pos_bits = (int)pos_bits.Value;
            settings.tex_bits = (int)tex_bits.Value;
            settings.col_bits = (int)col_bits.Value;
            settings.trn_bits = (int)trn_bits.Value;
            settings.rot_bits = (int)rot_bits.Value;
            settings.scl_bits = (int)scl_bits.Value;
            settings.anim_freq = (int)anim_freq.Value;
            settings.simplify_threshold = (float)simplify_threshold.Value;
            settings.texture_scale = (float)texture_scale.Value;
            settings.texture_limit = (int)texture_limit.Value;

            settings.pos_normalized = pos_normalized.IsChecked.Value;
            settings.pos_float = pos_float.IsChecked.Value;
            settings.anim_const = anim_const.IsChecked.Value;
            settings.keep_nodes = keep_nodes.IsChecked.Value;
            settings.keep_materials = keep_materials.IsChecked.Value;
            settings.keep_extras = keep_extras.IsChecked.Value;
            settings.mesh_merge = mesh_merge.IsChecked.Value;
            settings.mesh_instancing = mesh_instancing.IsChecked.Value;
            settings.simplify_aggressive = simplify_aggressive.IsChecked.Value;
            settings.simplify_lock_borders = simplify_lock_borders.IsChecked.Value;
            settings.texture_ktx2 = texture_ktx2.IsChecked.Value;
            settings.texture_pow2 = texture_pow2.IsChecked.Value;
            settings.texture_flipy = texture_flipy.IsChecked.Value;
            settings.quantize = quantize.IsChecked.Value;
            settings.compress = compress.IsChecked.Value;
            settings.compressmore = compressmore.IsChecked.Value;
            settings.fallback = fallback.IsChecked.Value;

            var ext = Path.GetExtension(dstPath);
            if (ext.ToLower() == ".glb")
                settings.texture_embed = true;

            loading.Visibility = System.Windows.Visibility.Visible;
            Task.Run(() =>
            {
                int ret = -1;
                try
                {
                    ret = GltfOptimizerApi.Process(srcPath, dstPath, null, settings);
                    OnProcessCompleted(ret, null);
                }
                catch (Exception ex)
                {
                    Log.Error(ex.Message);
                    OnProcessCompleted(ret, ex);
                }
            });

        }
    }
}
